<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.cese.duss.ceseutil.CostUtil" %>
<%@ page import="java.math.BigDecimal" %>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%
    String yskm = Util.null2String( request.getParameter( "yskm" ) );
    String sqje = Util.null2String( request.getParameter( "sqje" ) );
    String yshd = Util.null2String( request.getParameter( "yshd" ) );
    String sqrq = Util.null2String( request.getParameter( "sqrq" ) );
    String cblrzx = Util.null2String( request.getParameter( "cblrzx" ) );
    String requestid = Util.null2String( request.getParameter( "requestid" ) );
    bb.writeLog( "-----getTravelTrafficDtl_Ajax---requestid=" + requestid );
    bb.writeLog( "-----yshd=" + yshd );
    bb.writeLog( "-----sqrq=" + sqrq );
    bb.writeLog( "-----cblrzx=" + cblrzx );
    String yslx = Util.null2String( request.getParameter( "yslx" ) );
    JSONObject jsonData = new JSONObject();
    boolean flag = true;
    String msg = "";
    String jsonArr = Util.null2String( request.getParameter( "jsonArr" ) );
    JSONArray jsonArray = JSONArray.parseArray( jsonArr );
    JSONArray newjsonArray = delRepeatIndexid( jsonArray,yskm,sqje );
    String yskmName = "";
    for (int i = 0; i < newjsonArray.size(); i++) {
        String fykm =  newjsonArray.getJSONObject( i ).getString( yskm );////费用科目
        String hdje =  newjsonArray.getJSONObject( i ).getString( sqje );//核定金额
        BigDecimal newhdje = "".equals( hdje ) ? new BigDecimal( 0 ) : new BigDecimal( hdje );
        double jdcbkyys = CostUtil.getYszxxKyys( yshd, fykm, cblrzx, sqrq );
        double ysje = CostUtil.getYszxxxbYsje( requestid, yshd, fykm, cblrzx, sqrq, yslx );
        String fykmName = CostUtil.getFykmId2Name( fykm );//获取费用科目名称
        yskmName += "".equals( yskmName ) ? fykmName : "," + fykmName;
        bb.writeLog( "newhdje=" + newhdje );
        bb.writeLog( "ysje=" + ysje );
        bb.writeLog( "jdcbkyys=" + jdcbkyys );
        bb.writeLog( "fykmName=" + fykmName );
        if (jdcbkyys == 0) {
            flag = false;
            msg = "提示：费用科目为[" + yskmName + "]没有季度成本可用预算";
        } else {
            BigDecimal subval = new BigDecimal( jdcbkyys ).subtract( new BigDecimal( ysje ) );
            if (subval.compareTo( newhdje ) == -1) {
                flag = false;
                msg = "提示：预计费用不允许大于季度成本可用预算";
            }
        }

    }
    jsonData.put( "flag", flag );
    jsonData.put( "msg", msg );
    out.print( jsonData.toJSONString() );
    bb.writeLog( "------------getTravelTrafficDtl_Ajax=" + jsonData.toJSONString() );
%>
<%!
    //根据Key去重复合并值
    public static JSONArray delRepeatIndexid(JSONArray array,String kmparam,String jeparam) {
        JSONArray arrayTemp = new JSONArray();
        int num = 0;
        for (int i = 0; i < array.size(); i++) {
            if (num == 0) {
                arrayTemp.add( array.get( i ) );
            } else {
                int numJ = 0;
                for (int j = 0; j < arrayTemp.size(); j++) {
                    JSONObject newJsonObjectI = (JSONObject) array.get( i );
                    JSONObject newJsonObjectJ = (JSONObject) arrayTemp.get( j );
                    String index_idI = newJsonObjectI.get(kmparam).toString();
                    String valueI = newJsonObjectI.get( jeparam ).toString();
                    String index_idJ = newJsonObjectJ.get(kmparam ).toString();
                    String valueJ = newJsonObjectJ.get( jeparam).toString();
                    if (index_idI.equals( index_idJ )) {
                        String newValue = new BigDecimal( valueI ).add( new BigDecimal( valueJ ) ).toString();
                        arrayTemp.remove( j );
                        JSONObject newObject = new JSONObject();
                        newObject.put( kmparam, index_idI );
                        newObject.put(jeparam, newValue );
                        arrayTemp.add( newObject );
                        break;
                    }
                    numJ++;
                }
                if ((numJ - 1) == (arrayTemp.size() - 1)) {
                    arrayTemp.add( array.get( i ) );
                }
            }
            num++;
        }
        return arrayTemp;
    }
%>

